#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QGraphicsObject>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusMessage>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeContext>
#include <QtDeclarative/qdeclarative.h>

#include <contentaction/contentaction.h>

#include "qmlapplicationviewer.h"
#include "eventfeed.h"
#include "gconfitem.h"

#define DEBUG() qDebug() << __PRETTY_FUNCTION__ << __LINE__

class Server : public QObject
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "org.atnd")
public:
    explicit Server(QObject *parent = 0);

public slots:
    void launchWithLink(const QStringList &url);
    void activate();
};

Server::Server(QObject *parent)
    : QObject(parent)
{

}

void Server::launchWithLink(const QStringList &url)
{
    QVariant ret;

    DEBUG() << url;
    QmlApplicationViewer *viewer = qobject_cast<QmlApplicationViewer *>(parent());
    if (viewer) {
        QMetaObject::invokeMethod(viewer->rootObject(), "open", Q_RETURN_ARG(QVariant, ret), Q_ARG(QVariant, url));
    }
    activate();
}

void Server::activate()
{
    DEBUG();
    QmlApplicationViewer *viewer = qobject_cast<QmlApplicationViewer *>(parent());
    if (viewer) {
        viewer->showExpanded();
        viewer->activateWindow();
    }
}

class ActionHandler : public QObject
{
    Q_OBJECT
public:
    explicit ActionHandler(QObject *parent = 0);

public slots:
    void openUrlExternally(const QString &url);
};


ActionHandler::ActionHandler(QObject *parent)
    : QObject(parent)
{
}

void ActionHandler::openUrlExternally(const QString &url)
{
    ContentAction::Action::defaultActionForString(url).trigger();
}

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    qmlRegisterType<GConfItem>("ATND", 1, 0, "GConfItem");
    qmlRegisterType<EventFeed>("ATND", 1, 0, "EventFeed");
    viewer.engine()->rootContext()->setContextProperty("ActionHandler", new ActionHandler);
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/qatnd/main.qml"));

    if (app->arguments().length() == 1) {
        viewer.showExpanded();
    } else {
        EventFeed feed;
        feed.setIcon("/usr/share/icons/hicolor/80x80/apps/qatnd80.png");
        feed.setTitle("Checked");
        feed.setTimestamp(QDateTime::currentDateTime());
        feed.setSourceName("atnd");
        feed.setSourceDisplayName("ATND");
        DEBUG() << feed.add();
        QTimer::singleShot(10000, app.data(), SLOT(quit()));
    }

    if (app->arguments().length() == 1) {
        QDBusConnection connection = QDBusConnection::sessionBus();
        if (connection.registerService("org.atnd")) {
            if (connection.isConnected()) {
                connection.registerObject(QLatin1String("/org/atnd"), new Server(&viewer), QDBusConnection::ExportAllContents);

            }
        } else {
            QDBusMessage message = QDBusMessage::createMethodCall("org.atnd", "/org/atnd", "org.atnd", "activate");
            connection.call(message);
        }
    }
    return app->exec();
}

#include "main.moc"
